//
//  PHPasswordViewController.m
//  PhnixHome
//
//  Created by Levy Xu on 2018/1/9.
//  Copyright © 2018年 Levy Xu. All rights reserved.
//

#import "PHPasswordViewController.h"

@interface PHPasswordViewController ()<QMUITextFieldDelegate>
@property(nonatomic, strong) QMUITextField *pwdTextField1;
@property(nonatomic, strong) QMUITextField *pwdTextField2;
@property(nonatomic, strong) QMUITextField *pwdTextField3;
@end

@implementation PHPasswordViewController

- (void)initSubviews {
    [super initSubviews];
    
    _pwdTextField1 = [self textField:@"tips_input_old_password" icon:@"login_pwd"];
    [self.view addSubview:_pwdTextField1];
    
    _pwdTextField2 = [self textField:@"tips_input_new_password" icon:@"login_pwd"];
    [self.view addSubview:_pwdTextField2];
    
    _pwdTextField3 = [self textField:@"tips_input_password_again" icon:@"login_pwd"];
    [self.view addSubview:_pwdTextField3];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    _pwdTextField1.frame = CGRectMake(30.0f, self.qmui_navigationBarMaxYInViewCoordinator + 20, SCREEN_WIDTH - 2 * 30.0f, 40);
    _pwdTextField2.frame = CGRectMake(_pwdTextField1.qmui_left, _pwdTextField1.qmui_bottom + 10, _pwdTextField1.qmui_width, _pwdTextField1.qmui_height);
    _pwdTextField3.frame = CGRectMake(_pwdTextField1.qmui_left, _pwdTextField2.qmui_bottom + 10, _pwdTextField1.qmui_width, _pwdTextField1.qmui_height);
}

-(QMUITextField *)textField:(NSString *)placeholder icon:(NSString *)icon {
    QMUITextField *textField = [[QMUITextField alloc] init];
    textField.borderStyle = UITextBorderStyleNone;
    textField.returnKeyType = UIReturnKeyDone;
    textField.backgroundColor = UIColorClear;
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    textField.font = CodeFontMake(14);
    textField.textColor = UIColorBlack;
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.placeholder = NSLocalizedString(placeholder, nil);
    textField.secureTextEntry = YES;
    textField.delegate = self;
    textField.qmui_borderPosition = QMUIBorderViewPositionBottom;
    
    textField.enablesReturnKeyAutomatically = YES;
    [textField addTarget:self action:@selector(updateSubmitButtonEnables) forControlEvents:UIControlEventEditingChanged];
    
    UIImageView *pwdIV1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    pwdIV1.image = UIImageMake(icon);
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = pwdIV1;
    
    return textField;
}

- (void)updateSubmitButtonEnables {
    self.navigationItem.rightBarButtonItem.enabled = [NSString dd_checkPassword:_pwdTextField1.text] && [NSString dd_checkPassword:_pwdTextField2.text] && [NSString dd_checkPassword:_pwdTextField3.text];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [_pwdTextField1 becomeFirstResponder];
    [self updateSubmitButtonEnables];
}

-(void)setupNavigationItems {
    self.title = NSLocalizedString(@"modify_password", nil);
    self.navRightTitle = NSLocalizedString(@"nav_save", nil);
    
    [super setupNavigationItems];
}

-(void)rightAction {
    NSString *old_password = _pwdTextField1.text;
    NSString *new_password = _pwdTextField2.text;
    if (![new_password isEqualToString:_pwdTextField3.text]) {
        [KKHUD showError:NSLocalizedString(@"tips_password_not_same", nil)];
        [_pwdTextField3 becomeFirstResponder];
        return;
    }
    
    NSDictionary *parameters = @{ @"new_password": [new_password dd_md5],
                                  @"old_password": [old_password dd_md5] };
    LOG(@"parameters %@",parameters);
    
    [KKHUD showLoading:NSLocalizedString(@"tips_waiting", nil)];
    WS(ws);
    [[NetUtils JSONManager] POST:@"user/updatePassword.json" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        CommonResult *result = [CommonResult mj_objectWithKeyValues:[responseObject mj_JSONObject]];
        LOG(@"responseObject %@",responseObject);
        if (result.is_result_suc) {
            [KKHUD showSucceed:NSLocalizedString(@"tips_success", nil)];
            [ws.navigationController popViewControllerAnimated:YES];
        } else {
            [KKHUD showError:result.error_msg];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        LOG(@"error %@",error.description);
        [KKHUD showError:NSLocalizedString(@"tips_network_error", nil)];
    }];
}

@end
